package com.manuserve.mountainduck.kernel;

import java.util.*;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.*;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.View;
import com.google.android.gms.ads.AdRequest;
import com.manuserve.mountainduck.R;
import com.manuserve.mountainduck.MainActivity ;

public class  GameBoard extends View {
	 //private Paint p;

    private Graphics graphics ;
    public Univers univers ;
    public int bestscore ;
    public int bestpersonalscore ;
    long lastDisplayTime ;
    float decaSpriteX = 0f ;//Le décalage en cours de jeu du premier bloc en bas à gauche
    float decaSpriteY = 0f ;
    float decaY = 0 ; //le décalage du haut et du bas suite à l'adaptation à l'écran, pour que le plancher soit bien en bas
    float decaX  = 0 ;
    float decorX = 200 ;
    float decorY = 200 ;
    float decor2X = 800 ;
    float decor2Y = -200 ;
    float decor3X = 0 ;
    float decor3Y = 0 ;
    int duckAnimationStep = 0 ;
    int discrWidth = 9 ;
    int discrHeight = 12 ;
    boolean paused = false ;
    int bpm = 140 ;
    double startTme = 0 ;
    long pauseSetTime = 0 ; //pour mettre un timeout sur le readonly de la pause
    boolean animation = false ; //une animation en cours empeche le clic
    String bestauthor = "" ;
    MediaPlayer generiqueAudio = null ;
    MediaPlayer longSoundEffect = null ;
    MediaPlayer bingSoundEffect = null ;
    int indexSpriteCloud1 = 0 ;
    int indexSpriteCloud2 = 0 ;
    float caseWidth ;
    float moyFps = 30;
    Context context ;
    boolean ready = false ;
    PointF translationCamera ;
    boolean isStartScreen = true ;
    boolean isLostScreen = false ;
    public int score ;
    MainActivity activity ;
    boolean isAds = false ;
    boolean moche = false ;
    public int compteurPub = 0 ;

    Hashtable<Integer,Sprite> tabSprites = null ;

public GameBoard(Context context, AttributeSet aSet) {
    super(context, aSet);
    this.context = context;
    GameBoard thisIHM = this ;
    thisIHM.graphics = new Graphics() ;
 }

    public void init(MainActivity activite) {
        GameBoard thisIHM = this ;
        thisIHM.activity = activite ;
        thisIHM.graphics.width = this.getWidth() ;
        thisIHM.graphics.height = this.getHeight() ;
        float ratio = 1.775f ;
        if( thisIHM.graphics.height >= (1.75*thisIHM.graphics.width)) {
            //Ecran en mode page
            //On utilise toute le largeur disponible
            //la reso horizontable va fixer la taille des cases.
            thisIHM.caseWidth = (float) Math.floor(thisIHM.graphics.width/thisIHM.discrWidth) ;
            //thisIHM.discrHeight = (int) Math.floor(thisIHM.graphics.height/thisIHM.discrWidth ) ;

        } else {
            //Ecran en mode landscape
            //La hauteur va déterminer la largeur à utiliser
            thisIHM.caseWidth = (float) Math.floor((thisIHM.graphics.height/ratio )/thisIHM.discrWidth) ;
        }

        thisIHM.discrHeight = (int) Math.floor(thisIHM.graphics.height/thisIHM.caseWidth ) ;
        thisIHM.decaY = thisIHM.graphics.height - thisIHM.discrHeight*thisIHM.caseWidth ;
        thisIHM.decaX = (thisIHM.graphics.width - thisIHM.discrWidth*thisIHM.caseWidth)/2 ;
        thisIHM.startTme = Calendar.getInstance().getTimeInMillis() ;
        thisIHM.univers = new Univers(thisIHM);
        thisIHM.decor3X = thisIHM.decaX ;
        thisIHM.loadRessources();
        thisIHM.reset();
        thisIHM.loadPersonalBestScore();
        thisIHM.setBestScore();
        //this.messageStart();
        thisIHM.ready = true ;
        thisIHM.isStartScreen = true ;
        thisIHM.pauseSetTime = Calendar.getInstance().getTimeInMillis();
        thisIHM.longSoundEffect = MediaPlayer.create(this.getContext(), R.raw.bell);
        thisIHM.bingSoundEffect= MediaPlayer.create(this.getContext(), R.raw.bing);
        thisIHM.generiqueAudio = MediaPlayer.create(getContext(),R.raw.ducktheme);
        thisIHM.generiqueAudio.setLooping(true );
    }

    public boolean isReady() {
        return this.ready ;
    }




    public void loadRessources() {
        GameBoard thisIHM = this ;
        thisIHM.tabSprites = new Hashtable<Integer, Sprite>();
        thisIHM.tabSprites.put(0,new Sprite(this.getResources(),R.drawable.mountainduck, 300,300)) ;
        thisIHM.tabSprites.put(1,new Sprite(this.getResources(),R.drawable.case1,(int) thisIHM.caseWidth,(int) thisIHM.caseWidth)) ;
        thisIHM.tabSprites.put(2,new Sprite(this.getResources(),R.drawable.case2,(int) thisIHM.caseWidth,(int) thisIHM.caseWidth)) ;
        thisIHM.tabSprites.put(3,new Sprite(this.getResources(),R.drawable.credits1, 300,300)) ;
        thisIHM.tabSprites.put(4,new Sprite(this.getResources(),R.drawable.credits2, 300,300)) ;
        thisIHM.tabSprites.put(5,new Sprite(this.getResources(),R.drawable.duck_1,(int) thisIHM.caseWidth,(int) thisIHM.caseWidth)) ;
        thisIHM.tabSprites.put(6,new Sprite(this.getResources(),R.drawable.duck_2,(int) thisIHM.caseWidth,(int) thisIHM.caseWidth)) ;
        thisIHM.tabSprites.put(7,new Sprite(this.getResources(),R.drawable.cloud,600,300)) ;
        thisIHM.tabSprites.put(8,new Sprite(this.getResources(),R.drawable.mountain,(int) 2* thisIHM.graphics.width , (int) thisIHM.graphics.height/2 )) ;
        thisIHM.tabSprites.put(9,new Sprite(this.getResources(),R.drawable.cloud2,600,300)) ;
        thisIHM.tabSprites.put(10,new Sprite(this.getResources(),R.drawable.cloud3,600,300)) ;
        thisIHM.tabSprites.put(11,new Sprite(this.getResources(),R.drawable.cloud4,600,300)) ;
        thisIHM.indexSpriteCloud1 = 7 ;
        thisIHM.indexSpriteCloud2 = 7 ;
    }



    public void drawCanard() {
        GameBoard thisIHM = this ;
        Univers thisUnivers = thisIHM.univers ;
        thisIHM.duckAnimationStep = ( thisIHM.duckAnimationStep +1 ) % 2 ;
        thisIHM.displaySpriteFromUniversCoord(thisUnivers.duck.i,thisUnivers.duck.j,5+thisIHM.duckAnimationStep);
    }


     public void reset() {
        this.decaSpriteX = 0 ;
         this.decaSpriteY = 0 ;
    }

    public void pause() {
        this.univers.endJourney();
        this.generiqueAudio.stop();

       // try {
       //     this.generiqueAudio.prepare();
       // } catch(Exception e) {

        //}

        //this.generiqueAudio = MediaPlayer.create(getContext(),R.raw.ducktheme);
        //this.generiqueAudio.reset();

    }

    public void resume() {
        //this.generiqueAudio.stop();
    }

    public float getPhase() {
        double tempsPasseJeu = Calendar.getInstance().getTimeInMillis() -  this.startTme  ;
        float noire = 2000*60/this.bpm ;
        double ret = (tempsPasseJeu - noire*Math.floor(tempsPasseJeu/noire))/noire ;
        return (float) Math.cos(2*Math.PI*ret) ;
    }


    public float getPhaseMesure() {
        double tempsPasseJeu = (new Date().getTime() -  this.startTme ) ;
        float noire = 8*1000*60/this.bpm ;
        double ret = (tempsPasseJeu - noire*Math.floor(tempsPasseJeu/noire))/noire ;
        return (float) Math.cos(2*Math.PI*ret) ;
    }




    public void displaySpriteFromUniversCoord(float i,float j,int indexSprite) {
        GameBoard thisIHM = this ;
        float x = thisIHM.getCanvasXFromGameI(i);
        float y = thisIHM.getCanvasYFromGameJ(j);
        if(indexSprite == 0) {
           // return ;
        } else {
            thisIHM.graphics.drawSprite(thisIHM.tabSprites.get(indexSprite), x, y);
            if( ! moche  && ( indexSprite == 1 ||  indexSprite == 2 ) ) {
                float uniq = ((i*2203+j*2833)%7)/7 ;
                float phase = thisIHM.getPhase()*uniq ;
                float x1 = (float) -0.2*phase ;
                float y1 = (float) -0.2*phase ;
                float x_center = x+thisIHM.caseWidth/2 ;
                float y_center = y+thisIHM.caseWidth/2 ;
                int color = Color.YELLOW ;
                if( thisIHM.univers.jPointerIHM > 50 )
                    color = Color.rgb(255,127,0);
                if( thisIHM.univers.jPointerIHM > 100 )
                    color = Color.WHITE ;
                if( thisIHM.univers.jPointerIHM > 200 )
                    color = Color.RED ;
                if( thisIHM.univers.jPointerIHM > 500 )
                    color = Color.BLUE ;
                if( thisIHM.univers.jPointerIHM > 1000 )
                    color = Color.BLACK ;
                if( indexSprite == 1 ) {
                    float lineWidth = thisIHM.caseWidth/5 ;
                    thisIHM.graphics.drawRect(
                             (x_center+x1*thisIHM.caseWidth-lineWidth),
                             (y_center+y1*thisIHM.caseWidth-lineWidth),
                            (0.4f*phase*thisIHM.caseWidth+2f*lineWidth),
                            (0.4f*phase*thisIHM.caseWidth+2f*lineWidth),Color.parseColor("#77393c"),1.0f);
                    thisIHM.graphics.drawRect(
                             (x_center+x1*thisIHM.caseWidth),
                             (y_center+y1*thisIHM.caseWidth),
                            (0.4f*phase*thisIHM.caseWidth),
                            (0.4f*phase*thisIHM.caseWidth),color,0.4f);
                    //Effet mirroir
                    float sx = ( 3*(thisIHM.caseWidth - thisIHM.caseWidth * (x/thisIHM.graphics.width))) % thisIHM.caseWidth;
                    if( (x+sx-lineWidth/2) > ( x_center+thisIHM.caseWidth*x1-lineWidth/2) &&   (x+sx)< ( x_center-thisIHM.caseWidth*x1+lineWidth/2) ){
                        //thisIHM.graphics.drawRect(
                        //        x+sx,
                        //        Math.min(y_center+thisIHM.caseWidth*y1-lineWidth,y_center-lineWidth),
                        //        1,thisIHM.caseWidth*0.4f*phase+lineWidth ,Color.WHITE,0.4f);
                        thisIHM.graphics.drawRect(
                                        x_center+thisIHM.caseWidth*x1-lineWidth,
                                        y+sx,
                                        thisIHM.caseWidth*0.4f*phase+2*lineWidth , 1,Color.WHITE,1.0f);
                    }
                }
            }
        }
    }


    public float getGameIFromCanvasX(float x) {
        return (x-(this.decaX))/this.caseWidth + this.univers.iPointerIHM ;
    }
/*
    getGameJFromCanvasY : function(y) {
        var thisIHM = this ;
       return (thisIHM.graphics.height - y )/thisIHM.caseWidth + thisIHM.univers.jPointerIHM ;
    },
*/
    public float getCanvasXFromGameI(float i) {
        return this.decaX+(i-this.univers.iPointerIHM)*this.caseWidth ;
    }


    public float getCanvasYFromGameJ(float j) {
        return  this.decaY+this.caseWidth * (this.discrHeight - 1  )
                - (j-this.univers.jPointerIHM)*this.caseWidth ;
    }

    public float getUniversIFromDiscrI(float i) {
        float res = this.univers.iPointerIHM + i + this.decaSpriteX/this.caseWidth ;
        //Problemes constatés de passage avant l'unité, float javascripts horribles
        return Math.round(res*1000)/1000 ;
    }

    public float getUniversJFromDiscrJ(float j) {
        float res = this.univers.jPointerIHM + j - this.decaSpriteY/this.caseWidth ;
        //Problemes constatés de passage avant l'unité, float javascripts horribles
        return Math.round(res*1000)/1000 ;
    }

    public void alerteNiveau() {

        try {
            this.longSoundEffect = MediaPlayer.create(this.getContext(), R.raw.bell);
            this.longSoundEffect.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void alerteBing() {

        try {
           // this.bingSoundEffect.is
            this.bingSoundEffect.start();
        } catch (Exception e) {
           // e.printStackTrace();
        }
    }

    public void playAudioUrgence() {
        MediaPlayer audio = MediaPlayer.create(this.getContext(), R.raw.duckurgence);
        try {
            audio.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
               .build();
        this.activity.interstitial.loadAd(adRequest);
    }


    public void handleClick(float x,float y) {
        GameBoard thisIHM = this ;
        if(thisIHM.animation )
            return ;

        if(thisIHM.paused) {

            float deltaT =  (float) (Calendar.getInstance().getTimeInMillis()- thisIHM.pauseSetTime);

            if(thisIHM.isLostScreen) {
                //On a touché le podium ?
                float zx = (float) Math.floor((thisIHM.graphics.width-300)/2);
                float zy = (float) Math.floor((thisIHM.graphics.height-300)/2);
                if(x > zx && x < (zx+150) && y > (zy+140) && y < (zy+300) ) {
                    this.activity.handleScore(thisIHM.score);
                    return ;
                }
                //On a touché le bouton share ?
                if(x > (zx+150) && x < (zx+300) && y > (zy+150) && y < (zy+300) ) {
                    this.activity.handleShare("My duck has climbed " + thisIHM.score+" ft ! http://www.gracefulstorms.com/mountainduck/");
                    return ;
                }

            }


            if( deltaT < 100 ) {
                return ;
            }
            try {
                //thisIHM.generiqueAudio.prepare();
                this.generiqueAudio = MediaPlayer.create(getContext(),R.raw.ducktheme);
                this.generiqueAudio.setLooping(true );
                thisIHM.generiqueAudio.start();

            } catch (Exception e) {
                try {

                } catch (Exception e2) {

                }

            }

            compteurPub++ ;
            isAds = false ;
            thisIHM.paused = false ;
            thisIHM.isStartScreen = false ;
            thisIHM.isLostScreen = false ;
            thisIHM.lastDisplayTime = 0 ;
            return ;
        }
        float Ui  =  thisIHM.getGameIFromCanvasX(x);
        thisIHM.univers.changeCol((int) Math.floor(Ui));
    }




    public void drawStartScreen() {
        GameBoard thisIHM = this ;
        thisIHM.paused = true ;
        float x = (float) Math.floor((thisIHM.graphics.width-300)/2);
        float y = (float) Math.floor((thisIHM.graphics.height-547)/2);
        thisIHM.graphics.drawRect(0,0,thisIHM.graphics.width,thisIHM.graphics.height,Color.BLACK,1);
        thisIHM.graphics.drawSprite(thisIHM.tabSprites.get(0),x, y);
        thisIHM.graphics.drawSprite(thisIHM.tabSprites.get(3),x, y+300);
        thisIHM.graphics.drawText("[Touch to Continue]",x+50,y+700,25,Color.WHITE,Color.BLACK,0,1);
    }

    public void loadPersonalBestScore() {
            SharedPreferences mPrefs = this.context.getSharedPreferences("label",0);
            this.bestpersonalscore = mPrefs.getInt("bestscore",20);
        }

    public void setPersonalBestScore() {
        SharedPreferences mPrefs = this.context.getSharedPreferences("label",0);
        SharedPreferences.Editor ed = mPrefs.edit() ;
        ed.putInt("bestscore", this.bestpersonalscore).commit();
    }

    public void setBestScore() {
        //this.sendScore("a","0");
    }


    public void sendScore(String author,int score) {
        //var thisIHM = this ;
        //var xmlhttp=new XMLHttpRequest();
        //xmlhttp.onreadystatechange=function()
        //{
        //    if (xmlhttp.readyState==4) {
        //        var data  = xmlhttp.responseText.split(':');
        //        thisIHM.bestauthor = data[0];
        //        thisIHM.bestscore  = data[1];
        //    }
        //
        //      };
        //xmlhttp.open("GET","score.php?author=" + author+"&score="+score+"&rand="+Math.random(),true);
        // xmlhttp.send();
    }


    public void drawEndGameScreen() {
        GameBoard thisIHM = this ;

        if(!isAds) {
            if(score > 25 && compteurPub > 1 ) {
                if (activity.interstitial.isLoaded()) {
                    activity.interstitial.show();
                    compteurPub ++ ;
                }
            }
        }

        thisIHM.graphics.drawRect(0,0,thisIHM.graphics.width,thisIHM.graphics.height,Color.BLACK,1);
        if (score > thisIHM.bestpersonalscore)  {
            //alert(score+" : it's your own record !");
            thisIHM.bestpersonalscore = score ;
            thisIHM.setPersonalBestScore();
        }
        if(score > thisIHM.bestscore) {
            //thisIHM.bestauthor = prompt(score+" : it's the world record ! Who are you ?");
            thisIHM.bestscore = score ;
            thisIHM.sendScore(thisIHM.bestauthor,thisIHM.bestscore);
        }


        thisIHM.graphics.drawRect(0,0,thisIHM.graphics.width,thisIHM.graphics.height,Color.BLACK,1);

        float x = (float) Math.floor((thisIHM.graphics.width-300)/2);
        float y = (float) Math.floor((thisIHM.graphics.height-300)/2);

        thisIHM.graphics.drawSprite(thisIHM.tabSprites.get(4),x, y);

        thisIHM.graphics.drawText(String.valueOf(score),x+190,y+74,25,Color.WHITE,Color.BLACK,0,1);
        thisIHM.graphics.drawText(String.valueOf(thisIHM.bestpersonalscore),x+190,y+120,25,Color.WHITE,Color.BLACK,0,1);
        thisIHM.graphics.drawText("[Touch to Continue]",x+50,y+400,25,Color.WHITE,Color.BLACK,0,1);
        //thisIHM.graphics.drawText("See podium",x+190,y+164,20,Color.WHITE,Color.BLACK,0,1);
        //thisIHM.graphics.drawText(String.valueOf("See Podium"),x+190,y+200,20,Color.WHITE,Color.BLACK,0,1);
    }



    synchronized public void handleActionDown(float x,float y) {

        //float decaI = getRelativeLeft(this) ;
        //float decaJ = getRelativeTop(this) ;
        //float i = x-decaI;
        //float j = y-decaJ;
        //this.handleClick(i,j);
        this.handleClick(x,y);

}

    private int getRelativeLeft(View myView) {
        if (myView.getParent() == myView.getRootView())
            return myView.getLeft();
        else
            return myView.getLeft() + getRelativeLeft((View) myView.getParent());
    }

    private int getRelativeTop(View myView) {
        if (myView.getParent() == myView.getRootView())
            return myView.getTop();
        else
            return myView.getTop() + getRelativeTop((View) myView.getParent());
    }






    public void update() {
        GameBoard thisIHM = this ;
        if(thisIHM.paused) {
            return ;
        }
        Univers thisUnivers = thisIHM.univers ;
        this.translationCamera = new PointF(0,0) ;
        long timeIntervall = 0  ;
        if(thisIHM.lastDisplayTime == 0) {
            thisIHM.lastDisplayTime =  Calendar.getInstance().getTimeInMillis();
        } else {
            long newDisplayTime = Calendar.getInstance().getTimeInMillis();
            timeIntervall = newDisplayTime - thisIHM.lastDisplayTime ;
            thisIHM.lastDisplayTime = newDisplayTime ;
            this.translationCamera = thisUnivers.physics(timeIntervall);
        }
        if(timeIntervall> 0) {
            moyFps = (50 * moyFps + ((float) 1000.0 / (float) timeIntervall)) / 51;
        }
        if(moyFps < 40) {
            moche = true ;
        }

        if(thisIHM.paused) { //Le jeu peut avoir été mis en pause apres physique
            return ;
        }
        thisIHM.decaSpriteX-=this.translationCamera.x*thisIHM.caseWidth ; //negatif
        thisIHM.decaSpriteY+=this.translationCamera.y*thisIHM.caseWidth ; //positif
        //boolean isRecadrage = false ;
        while( thisIHM.decaSpriteX < -thisIHM.caseWidth ) {
            thisIHM.decaSpriteX+=thisIHM.caseWidth;
            //isRecadrage = true ;
        }
        while( thisIHM.decaSpriteY > thisIHM.caseWidth ) {
            //isRecadrage = true ;
            thisIHM.decaSpriteY=thisIHM.decaSpriteY-thisIHM.caseWidth;
        }

    }



    @Override
       synchronized public void onDraw(Canvas canvas) {
        if(!this.ready )
            return ;
        this.redraw(canvas);
     }

    public int getIdSpriteNuage() {
        GameBoard thisIHM = this ;
        Univers thisUnivers = thisIHM.univers ;
        if( thisUnivers.jPointerIHM > 200 )
            return 11 ;
        if( thisUnivers.jPointerIHM > 100 )
            return 10 ;
        if( thisUnivers.jPointerIHM > 50 )
            return 9 ;
        return 7 ;
    }

    public void redraw(Canvas canvas) {
        GameBoard thisIHM = this ;
        Univers thisUnivers = thisIHM.univers ;

        thisIHM.graphics.setCanvas(canvas,this.getWidth(),this.getHeight());
        //Gradient

        if(this.isStartScreen) {
            thisIHM.drawStartScreen();
            return ;
        }

        if(this.isLostScreen) {
            thisIHM.drawEndGameScreen();
            return ;
        }

        thisIHM.graphics.drawGradientTwoColors(
                thisIHM.decaX,
                0,
                thisIHM.graphics.width,
                thisIHM.graphics.height,
                Color.parseColor("#C1F0F6"),
                Color.parseColor("#A893CB")
        );

        //Nuages

        float phase = thisIHM.getPhaseMesure() ;
        float scaling1 = 0.9f + 0.1f*phase ;
        float scaling2 = 0.9f + 0.1f*phase ;

        thisIHM.decorX-=this.caseWidth*this.translationCamera.x/3;
        thisIHM.decorY+=this.caseWidth*this.translationCamera.y/6;
        if( thisIHM.decorX < ( thisIHM.decaX/2 -  800) || thisIHM.decorY > thisIHM.graphics.height) {
            thisIHM.decorX = thisIHM.graphics.width - thisIHM.decaX/2;
            thisIHM.decorY = -300 ;
            thisIHM.indexSpriteCloud1 = getIdSpriteNuage();

        }
        float montagneH = thisIHM.graphics.height/2 ;
        float montagneW =  (thisIHM.graphics.height/2)*700/300;
        thisIHM.decor3Y = thisIHM.graphics.height - montagneH  ;
        thisIHM.graphics.drawSprite(thisIHM.tabSprites.get(thisIHM.indexSpriteCloud1),thisIHM.decorX+300*(1-scaling1),thisIHM.decorY+150*(1-scaling1));
        //thisIHM.decor2X-=0.5;
        //thisIHM.decor2Y+=0.25;
        thisIHM.decor2X-=this.caseWidth*this.translationCamera.x/3;
        thisIHM.decor2Y+=this.caseWidth*this.translationCamera.y/6;
        if( thisIHM.decor2X < ( thisIHM.decaX/2 -  800)|| thisIHM.decor2Y > thisIHM.graphics.height) {
            thisIHM.decor2X = thisIHM.graphics.width - thisIHM.decaX/2;
            thisIHM.decor2Y = -300 ;
            thisIHM.indexSpriteCloud2 = getIdSpriteNuage();
        }

        thisIHM.graphics.drawSprite(thisIHM.tabSprites.get(thisIHM.indexSpriteCloud2),thisIHM.decor2X+300*(1-scaling2),thisIHM.decor2Y+150*(1-scaling2));
        //montagne
        thisIHM.decor3X-=this.caseWidth*this.translationCamera.x/2;


        if(thisIHM.decor3X< ( (thisIHM.decaSpriteX/2) - montagneW  ) ) {
            thisIHM.decor3X+=montagneW ;
        }
        thisIHM.graphics.drawSprite(thisIHM.tabSprites.get(8),
                thisIHM.decor3X,
                thisIHM.decor3Y
        );

        thisIHM.graphics.drawSprite(thisIHM.tabSprites.get(8),
                montagneW+thisIHM.decor3X,
                thisIHM.decor3Y
        );

        //La colline

        for(int i=0;i<=thisIHM.discrWidth;i++) {
            for(int j=0;j<=thisIHM.discrHeight;j++) {
                float iUnivers = thisIHM.getUniversIFromDiscrI(i) ;
                float jUnivers = thisIHM.getUniversJFromDiscrJ(j) ;
                thisIHM.displaySpriteFromUniversCoord(iUnivers, jUnivers, thisUnivers.spriteInfoProvider(iUnivers, jUnivers));
            }
        }

        //Score
        thisIHM.graphics.drawText((int) Math.floor(thisIHM.univers.jPointerIHM)+" ft",thisIHM.decaX+20,100,36,Color.YELLOW,Color.BLACK,1,1);

        //thisIHM.graphics.drawText(Float.toString(thisUnivers.duck.bitchness),thisIHM.decaX+20,300,36,Color.RED,Color.BLACK,1,1);



        //Tuto
        float tutoCoeff  = ((20-thisIHM.univers.jPointerIHM)/20)*(1+thisIHM.getPhase())/4;
        if(tutoCoeff > 0 && thisUnivers.selectedCol != thisUnivers.duck.trajectoire.toI ) {

            //Trajectoire
            float destX =  thisIHM.getCanvasXFromGameI(thisUnivers.duck.trajectoire.toI);
            float destY =  thisIHM.getCanvasYFromGameJ(thisUnivers.duck.trajectoire.toJ-1);
            PointF controlPoint = thisUnivers.duck.trajectoire.getControlPoint() ;
            thisIHM.graphics.setGradientTwoColors(
                    thisIHM.decaX,
                    0,
                    thisIHM.graphics.width-thisIHM.decaX,
                    thisIHM.graphics.height,
                    Color.YELLOW,
                    Color.WHITE) ;
            thisIHM.graphics.drawQuadraticFromCompiledPaint(
                    thisIHM.getCanvasXFromGameI(thisUnivers.duck.trajectoire.originI + 0.5f),
                    thisIHM.getCanvasYFromGameJ(thisUnivers.duck.trajectoire.originJ - 1) - 4,
                    thisIHM.getCanvasXFromGameI(controlPoint.x + 0.5f),
                    thisIHM.getCanvasYFromGameJ(controlPoint.y - 1) - 4,
                    thisIHM.getCanvasXFromGameI(thisUnivers.duck.trajectoire.toI + 0.5f),
                    thisIHM.getCanvasYFromGameJ(thisUnivers.duck.trajectoire.toJ - 1) - 4,
                    3,
                    tutoCoeff
            );
            float font =  Math.round(thisIHM.caseWidth*1.4/5) ;
            thisIHM.graphics.drawText("TOUCH",destX,destY- thisIHM.caseWidth/10,font,Color.YELLOW,
                    Color.BLACK,1,tutoCoeff);
            thisIHM.graphics.drawRect(2+destX,
                    2+destY,
                    thisIHM.caseWidth-4,
                    thisIHM.graphics.height,Color.YELLOW,tutoCoeff);
        }

        float oldx = 0 ;
        float oldy = 0 ;
        //Halo de bord
        List<PointF> tabCoords = new ArrayList<PointF>();

        for(int i=0;i<=thisIHM.discrWidth;i++) {
            float iAbs = thisIHM.getUniversIFromDiscrI(i);
            float j = 0f ;
            j = thisIHM.univers.data.get((int) Math.floor(iAbs)).height ;
            float x = thisIHM.getCanvasXFromGameI(iAbs);
            float y = thisIHM.getCanvasYFromGameJ(j);
            if(i==0) {
                tabCoords.add(new PointF(x,y));
            } else {
                tabCoords.add(new PointF(oldx+thisIHM.caseWidth,oldy));
                tabCoords.add(new PointF(x,y));
            }
            oldx = x ;
            oldy = y ;
        }

        /*
        if( ! moche) {
            thisIHM.graphics.drawSegments(tabCoords,Color.parseColor("#B2B8DC"),thisIHM.caseWidth/8,0.2f);
            thisIHM.graphics.drawSegments(tabCoords,Color.parseColor("#B2B8DC"),thisIHM.caseWidth/5,0.2f);
        }*/


        //Le canard
        thisIHM.drawCanard();

        //La vignette
        if( true) {
            thisIHM.graphics.vignette(thisIHM.decaX,thisIHM.graphics.width-2*thisIHM.decaX,this.getPhaseMesure());
        }



    }

}
